package com.func;

import com.Server;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class WeightedRandom extends Request{
    @Override
    Server getServer() {
        List<Server> list = getList();
        List<Server> weightedList = new ArrayList<>();
        int sum = 0;
        for (Server server : list) {
            sum += server.getWeight();
            for (int i = 0; i < server.getWeight(); i++) {
                weightedList.add(server);
            }
        }

        int index =  new Random().nextInt(sum);
        return weightedList.get(index);

    }
}
